/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RosaDosVentosVelocidades.java
 *
 * Created on 18/05/2010, 17:30:35
 */

package br.leiam.eolica.graficos;

import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jfree.chart.ChartUtilities;

import sun.misc.BASE64Encoder;
import br.leiam.eolica.BD.Consulta;
import br.leiam.eolica.excecoes.EolicaException;

/**
 * 
 * @author Denis
 */
public class RosaDosVentosVelocidades extends javax.swing.JPanel {
	private RosaDosVentosVelocidadesDirecao direcao;
	private Consulta tableModel;
	private String QUERY = "";
	private boolean haveData = false;

	public static String getColunaVelocidade(int altura) throws EolicaException {

		if (altura == 13) {
			return "p1a";
		} else if (altura == 100) {
			return "s2a";
		} else if (altura == 70) {
			return "s3a";
		} else if (altura == 102) {
			return "s1a";
		} else {
			throw new EolicaException(
					"Nao existe medicao de direcao na altura de " + altura
							+ " metros");
		}
	}

	public static String getColunaDirecao(int altura) throws EolicaException {

		if (altura == 13) {
			return "r1a";
		} else if (altura == 100) {
			return "d1a";
		} else if (altura == 70) {
			return "d2a";
		} else {
			throw new EolicaException(
					"Nao existe medicao de direcao na altura de " + altura
							+ " metros");
		}
	}

	/**
	 * Creates new form RosaDosVentosVelocidades
	 * 
	 * @throws Exception
	 */
	public RosaDosVentosVelocidades(String TABLE_NAME, String DataInicio,
			String DataFim, int altura, int tamx, int tamy)
			throws EolicaException {

		initComponents();
		this.setSize(tamx, tamx + 20);
		String ajuste = "/10";
		String coluna1 = getColunaVelocidade(altura);
		String coluna2 = getColunaDirecao(altura);
		// coluna1 = altura;
		// coluna2 = dir;
		if (coluna1.equals("p1a"))
			ajuste = "*0.0008+0.22";
		direcao = new RosaDosVentosVelocidadesDirecao();

		for (int x = 15; x < 345; x = x + 30) {
			String um = String.valueOf(x);
			String dois = String.valueOf(x + 30);
			QUERY = QUERY + "select '" + um + "-" + dois + "', (select avg("
					+ coluna1 + ")" + ajuste + " from " + TABLE_NAME
					+ " where " + coluna2 + " >" + um + " and " + coluna2
					+ " <" + dois + " and datahora between timestamp('"
					+ DataInicio + "') and timestamp('" + DataFim
					+ "')) as velocidade union \n";
			// QUERY = QUERY +
			// "select '"+um+"-"+dois+"', (select avg("+coluna1+")/10 from "+TABLE_NAME+" where "+coluna2+" between "+um+" and "+dois+" and datahora between timestamp('"+DataInicio+"') and timestamp('"+DataFim+"')) as velocidade union \n";
		}
		String um = String.valueOf(345);
		String dois = String.valueOf(375);
		String umm = String.valueOf(345 * Math.PI / 180);
		String doiss = String.valueOf(375 * Math.PI / 180);
		QUERY = QUERY + "select '" + um + "-" + dois + "', (select avg("
				+ coluna1 + ")" + ajuste + " from " + TABLE_NAME
				+ " where sin(3.1416*" + coluna2 + "/180) > sin(" + umm
				+ ") and sin(3.1416*" + coluna2 + "/180) < sin(" + doiss
				+ ") and datahora between timestamp('" + DataInicio
				+ "') and timestamp('" + DataFim + "')) as velocidade ";
		System.out.println(QUERY);
		tableModel = new Consulta(QUERY);
		direcao.setTableModel(tableModel);

		if (tableModel.getNumeroDeLinhas() > 0)
			haveData = true;

		jPanel3.add(direcao);
		direcao.repaint();
		Font fonte = jLabel5.getFont();
		float tamanhoFonte = this.getWidth() * 12 / 300;
		jLabel5.setFont(fonte.deriveFont(tamanhoFonte));
		jPanel4.setBounds(0, 0, this.getWidth(), 20 + (int) tamanhoFonte);
		jLabel5.setBounds(0, 0, jPanel4.getWidth(), 15);
		jLabel1.setBounds(0, jPanel4.getHeight() - 15, jPanel4.getWidth(), 15);
		jLabel3.setBounds(0, this.getHeight() - 15, this.getWidth(), 15);
		jPanel5.setBounds(0, jPanel4.getHeight(), 15, this.getHeight()
				- jPanel4.getHeight() - jLabel3.getHeight());
		jLabel2.setBounds(0, 0, jPanel5.getWidth(), jPanel5.getHeight());
		jLabel4.setBounds(this.getWidth() - 15, jPanel4.getHeight(), 15,
				jPanel5.getHeight());
		jPanel3.setBounds(jPanel5.getWidth(), jPanel4.getHeight(),
				this.getWidth() - jPanel5.getWidth() - jLabel4.getWidth(),
				this.getHeight() - jPanel4.getHeight() - jLabel3.getHeight());
		direcao.setBounds(2, 2, jPanel3.getWidth() - 4, jPanel3.getHeight() - 4);
	}

	public String getBase64String() {
		String figura = "";

		try {
			double escalaX = 1, escalaY = 1;
			Component co = this;
			BufferedImage bi = new BufferedImage(
					(int) (co.getSize().getWidth() * escalaX), (int) (co
							.getSize().getHeight() * escalaY),
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = bi.createGraphics();
			g2d.scale(escalaX, escalaY);
			co.paint(g2d);

			byte[] figBytes;

			figBytes = ChartUtilities.encodeAsPNG(bi);

			BASE64Encoder encoder = new BASE64Encoder();
			figura = encoder.encode(figBytes);
		} catch (Exception ex) {
			Logger.getLogger(Weibull.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return figura;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanel3 = new javax.swing.JPanel();
		jPanel4 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jPanel5 = new javax.swing.JPanel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();

		setBackground(new java.awt.Color(255, 255, 255));
		setPreferredSize(new java.awt.Dimension(500, 500));

		jPanel3.setBackground(new java.awt.Color(255, 255, 255));
		jPanel3.setLayout(new java.awt.BorderLayout());

		jPanel4.setBackground(new java.awt.Color(255, 255, 255));
		jPanel4.setOpaque(false);

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel1.setText("N");

		jLabel5.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
		jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel5.setText("VELOCIDADE MEDIA POR DIRECAO");

		javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(
				jPanel4);
		jPanel4.setLayout(jPanel4Layout);
		jPanel4Layout.setHorizontalGroup(jPanel4Layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jLabel1,
						javax.swing.GroupLayout.Alignment.TRAILING,
						javax.swing.GroupLayout.DEFAULT_SIZE, 520,
						Short.MAX_VALUE)
				.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
						520, Short.MAX_VALUE));
		jPanel4Layout
				.setVerticalGroup(jPanel4Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel4Layout
										.createSequentialGroup()
										.addComponent(jLabel5)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												23, Short.MAX_VALUE)
										.addComponent(jLabel1)));

		jPanel5.setBackground(new java.awt.Color(255, 255, 255));

		jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
		jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		jLabel2.setText("W");

		javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(
				jPanel5);
		jPanel5.setLayout(jPanel5Layout);
		jPanel5Layout.setHorizontalGroup(jPanel5Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 20,
				Short.MAX_VALUE));
		jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel5Layout
						.createSequentialGroup()
						.addComponent(jLabel2,
								javax.swing.GroupLayout.DEFAULT_SIZE, 309,
								Short.MAX_VALUE).addContainerGap()));

		jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
		jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel3.setText("S");
		jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jLabel3MouseClicked(evt);
			}
		});

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		jLabel4.setText("E");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(
										javax.swing.GroupLayout.Alignment.TRAILING,
										layout.createSequentialGroup()
												.addContainerGap()
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
																.addComponent(
																		jLabel3,
																		javax.swing.GroupLayout.Alignment.LEADING,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		500,
																		Short.MAX_VALUE)
																.addGroup(
																		layout.createSequentialGroup()
																				.addComponent(
																						jPanel5,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addPreferredGap(
																						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																				.addComponent(
																						jPanel3,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						450,
																						Short.MAX_VALUE)
																				.addPreferredGap(
																						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																				.addComponent(
																						jLabel4,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						20,
																						javax.swing.GroupLayout.PREFERRED_SIZE)))
												.addContainerGap())));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jPanel4,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(369, Short.MAX_VALUE))
				.addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(
										layout.createSequentialGroup()
												.addGap(70, 70, 70)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
																.addComponent(
																		jPanel3,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		320,
																		Short.MAX_VALUE)
																.addComponent(
																		jPanel5,
																		javax.swing.GroupLayout.Alignment.LEADING,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		jLabel4,
																		javax.swing.GroupLayout.Alignment.LEADING,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		320,
																		Short.MAX_VALUE))
												.addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(
														jLabel3,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														26,
														javax.swing.GroupLayout.PREFERRED_SIZE))));
	}// </editor-fold>//GEN-END:initComponents

	private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jLabel3MouseClicked

	}// GEN-LAST:event_jLabel3MouseClicked

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JPanel jPanel5;
	// End of variables declaration//GEN-END:variables
	
	/**
	 * @return the haveData
	 */
	public boolean isHaveData() {
		return haveData;
	}

	/**
	 * @param haveData
	 *            the haveData to set
	 */
	public void setHaveData(boolean haveData) {
		this.haveData = haveData;
	}

}
